import { _decorator, Component, director, Label, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LifeScript')
export class LifeScript extends Component {


    // @property(ProgressBar)
    // staminaBar: ProgressBar = null; // 体力进度条

    @property(Label)
    staminaText: Label = null; // 体力文本显示

    @property(Label)
    countdownLabel: Label = null; // 倒计时文本显示

    currentStamina: number = 3; // 当前体力
    maxStamina: number = 5; // 最大体力
    restoreRate: number = 1; // 每次恢复的体力
    restoreInterval: number = 300; // 每5分钟恢复1点体力，单位秒（300秒）
    countdownTime: number = 10; // 5分钟倒计时，单位秒

    restoreTimer: Node = null; // 体力恢复定时器
    countdownTimer: Node = null; // 倒计时定时器

    onLoad() {
        this.updateStaminaUI();
        // this.updateCountdownUI();
        this.startStaminaRecovery();
    }

    //开启定时器
    startStaminaRecovery() {
        // 以秒为单位的时间间隔
        let interval = 1;
        this.schedule(this.callback, interval);
    }

    callback() {
        if (this.currentStamina >= 5) {
            // 在第六次执行回调时取消这个计时器
            this.unschedule(this.callback);
        }
        this.updateStaminaUI();

    }


    updateStaminaUI() {
        if(this.currentStamina == 5){
            this.countdownLabel.string = 'MAX'
            return 
        }
        //更新定时器
        this.countdownTime--;
        let timeStr = this.formatTime(this.countdownTime)
        
        this.countdownLabel.string = timeStr
        //更新数量
        if(this.countdownTime == 0 && this.currentStamina < this.maxStamina){
            this.currentStamina += this.restoreRate;
            this.countdownTime = 300
            if (this.currentStamina > this.maxStamina) {
                this.currentStamina = this.maxStamina;
            }
            console.log(`当前体力值：${this.currentStamina}`)

            //动态加载
        }
        
    }


    formatTime(seconds: number): string {
        const minutes = Math.floor(seconds / 60);  // 计算分钟数
        const remainingSeconds = seconds % 60;    // 计算剩余的秒数
        if(remainingSeconds<10){
            return `0${minutes}:0${remainingSeconds}`;
        }else{
            return `0${minutes}:${remainingSeconds}`;
        }
       
    }


}


